function latexTableFlex(table, pdfName, useIndicator, options)
% Wrapper function for makeLatexTableCont which allows the user more
% flexibility in opening a new latex file, appending tables, adding
% figures, and closing the existing TeX file.
% ====================================================================
% [Input]:
% table.dataMat: matrix of data
% table.rowLabels:  cell array of row titles
% table.colLabels:  cell array of column title(on the top); 
% table.corner:  string, upper left corner of the graph
% table.tableHeader: string of Title name printed on the top of table
%
% pdfName: [1 2] Cell with {'pdfName.pdf' 'pdfCaption'}.  The
%          user needs to add the file extension '.pdf.
%
% useIndicator: (structure with boolean fields) controls use of function
%   .open:
%   .writeTable:
%   .addPDF
%   .close:
%
% [Options]:
% options.printPDF
% options.fileName: file name to write or append to
% options.outPath: location of directory for output
% options.orientation: orientation of table, 'portrait' or 'landscape'
%
% [Notes]:
% If the user just wants to add a table to a Tex file, set pdfName = {} (and
% useIndicator.writeTable = true)
% If the user just wants to add a pdf file, set dataMat = [] (and
% useIndicator.addPdf = true)
% =======================================================================
%% Check Settings

cwd=cd;
if isempty(table)
    useIndicator.writeTable = false;
end

if isempty(pdfName)
    useIndicator.addPDF = false;
end

if ~isfield(options,'outPath')
    options.outPath = cd;
end

fileNameFlag = false;
if ~isfield(options,'fileName')
    fileNameFlag = true;
    options.fileName = 'newTexFile';
end

if ~useIndicator.close && options.printPDF
    display('Need to close file before printing; supressing print option');
    options.printPDF= false;
end

fileName = options.fileName;
outPath = options.outPath;
%% Write to Latex

% Write the opening part of Latex File
if useIndicator.open

    tempTable.dataMat = [];
    tempOptions = options;
    tempOptions.open = true;
    tempOptions.close = false;
    tempOptions.printPDF = true;
    fid = makeLatexTableCont(tempTable,tempOptions);
    fclose(fid);
    
end

% Write a table to TeX file
if useIndicator.writeTable
    
    tempTable = table;
    tempOptions = options;
    tempOptions.open = false;
    tempOptions.append = true;
    tempOptions.close = false;
    fid = makeLatexTableCont(tempTable,tempOptions);
    fclose(fid);

end

% Lines to add PDF to TeX file
if useIndicator.addPDF
    
    fid = fopen([outPath, '\',fileName,'.tex.'],'a');
    fprintf(fid, '\\begin{figure}[t!] \n');
        fprintf(fid, ['\\caption{',pdfName{2},'} \n']);
    fprintf(fid, '\\begin{center} \n');
    fprintf(fid, ['\\includegraphics{',pdfName{1},'} \n']);
    fprintf(fid, '\\end{center} \n');
    fprintf(fid,'\\end{figure} \n');
    fprintf(fid,'\\pagebreak \n');
    fclose(fid);
   
end

% Closing Latex file
if useIndicator.close
    
    fid = fopen([outPath, '\\',fileName,'.tex.'],'a');
    fprintf(fid, '\\end{document}');
    fclose(fid);
    
end

% Move created file to designated folder, and optionally print PDF
if options.printPDF
    
    [status,result] = system(['pdflatex ', [fileName,'.tex'], ' -quiet']);
    [success_pdf,message_pdf]=movefile([fileName,'.tex'],outPath);
    [success_pdf,message_pdf]=movefile([fileName,'.pdf'],outPath);
    delete([fileName,'.aux'], [fileName,'.log']);
    
else
    
    [success_pdf,message_pdf]=movefile([fileName,'.tex'],outPath);
    
end

end
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

